package com.tanhua.server.controller;

import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.TodayBest;
import com.tanhua.server.service.MovementsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.util.List;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-12 02:05
 **/

@RestController
@RequestMapping("/movements")
public class MovementsController {


    @Autowired
    private MovementsService movementsService;

    /*
    * 发布动态
    *
    * Path： /movement
        Method： POST
    *请求参数：
    *   textContent,imageContent[](文件),location,longitude,latitude
    *
    * */
    @PostMapping
    public ResponseEntity movements(MultipartFile imageContent[],String textContent,
                                    String location,String longitude,String latitude) throws IOException {
        //调用service保存
        movementsService.movements(imageContent,textContent,location,longitude,latitude);
        return ResponseEntity.ok(null);
    }


    /*
    *
    * Path： /movements/all
        Method： GET
    * 查看个人动态
        参数(query)：
              page,pageSize,userId
       返回值：
           PageResult  ---List<MovementVo>
     */
    @GetMapping("/all")
    public ResponseEntity all( @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               Long userId){
        //1.获取参数
        //2.调用service
        PageResult vo = movementsService.all(page,pageSize,userId);
        //3.响应数据
        return ResponseEntity.ok(vo);
    }


    /*
    *查询好友动态
    * 接口路径：GET/movements
    * 参数(query)：
         page,pageSize,
         返回值：
         pageResult -- List<MovementVo>

    *
    * */
    @GetMapping
    public ResponseEntity movements(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize){


        PageResult vo  =   movementsService.findByFriendsId(page,pageSize);
        return ResponseEntity.ok(vo);
    }

    /*
    * 查看推荐动态
    * Path： /movements/recommend
    * Method： GET
    * 参数(query)：
         page,pageSize,
      返回值：
         pageResult -- List<MovementVo>
    *
    * */
    @GetMapping("/recommend")
    public ResponseEntity recommend(@RequestParam(defaultValue = "1") Integer page,
                                    @RequestParam(defaultValue = "10") Integer pageSize){

        PageResult vo = movementsService.findByRecommend(page,pageSize);
        return ResponseEntity.ok(vo);
    }



    /*
        根据id查询动态
    Path： /movements/{id}
    Method： GET
    *   参数：路径参数(动态id)
    *   返回值：MovementVo
    * */
    @GetMapping("/{id}")
    public  ResponseEntity findById(@PathVariable("id") String id){
        MovementsVo vo =  movementsService.findById(id);
        return ResponseEntity.ok(vo);
    }


    /*
    * 首页，谁看过我功能
    * */
    @GetMapping("/visitors")
    public  ResponseEntity visitors(){
        List<TodayBest> list = movementsService.visitors();
        return ResponseEntity.ok(list);
    }

    /*
    *  点赞动态：
    *   接口路径：GET /:id/like
    *   路径参数：动态id
    *   返回值(文档中返回值没有定义返回名称)：
    *       点赞数量
    *
    * */
    @GetMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String id){
        //1.调用service点赞，返回点赞数量
       Integer count =  movementsService.like(id);
       return ResponseEntity.ok(count);

    }

    /*
    * 取消点赞：
    *   接口路径： GET  /:id/dislike
    *   路径参数：动态id
    *   返回值(文档中返回值没有定义返回名称)：
    *       点赞数量
    *
    * */
    @GetMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String id){
        Integer count =  movementsService.dislike(id);
        return ResponseEntity.ok(count);
    }

    //喜欢
    @GetMapping("/{movementId}/love")
    public ResponseEntity love(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.love(movementId);
        return ResponseEntity.ok(count);
    }

    /**
     * 取消喜欢
     */
    @GetMapping("/{movementId}/unlove")
    public ResponseEntity unlove(@PathVariable("movementId") String movementId) {
        Integer count = movementsService.unlove(movementId);
        return ResponseEntity.ok(count);
    }





}
